import Cookies from 'js-cookie'

const cookieId = "JSESSIONID"
const user = {
    state: {
        id: 1,
        username: "",
        name: "",
        token: Cookies.get(cookieId), // 获取名为 JSESSIONID 的 cookie 数据 作为token使用
        code: "",
        sex: null,
        phone: "",
        delivery: false,
        password: "",
    },
    getters: {
        //获取id
        getId(state) {
            console.log("getId:" + state.id);
            return state.id;
        },
        getCode(state){
            return state.code;
        },
        getUsername(state) {
            return state.username;
        },
        getToken(state) {
            return state.token;
        },
        getName(state) {
            return state.name;
        },
        getSex(state) {
            return state.sex;
        },
        getPhone(state) {
            return state.phone;
        },
        getPassword(state) {
            return state.password;
        },
    },
    mutations: {
        saveUser(state, user) {
            state.username = user.username;
            state.name = user.name;
            state.phone = user.phone;
            state.sex = user.sex;
            state.delivery = user.delivery;
            state.password = user.password;
            state.id = user.id;
        },
        //保存id
        saveId(state, id) {
            state.id = id
        },
        saveCode(state, code) {
            state.code = "data:image/jpeg;base64," + code;
        },
        saveUsernameAndId(state, user) {
            state.id = user.id
            state.username = user.username
        },
        saveToken(state) {
            state.token = Cookies.get(cookieId)
        },
        removeToken(state) {
            state.token = ''
        },
        saveUsername(state, username){
            state.username = username;
        },
        savePhone(state, phone) {
            state.phone = phone;
        },
        saveSex(state, sex) {
            state.sex = sex;
        },
        saveName(state, name) {
            state.name = name;
        }
    },
    actions: {}
}
export default user
